package com.sikeda.imagecomposetool.utils;

import android.graphics.Bitmap;
import android.widget.ImageView;


import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.sikeda.imagecomposetool.R;

/**
 * Created by Administrator on 2016/8/9.
 */
public class ImageLoaderUtil {
    public static void setImageWithCache(String imgUrl, ImageView imageView) {
        //显示图片的配置
        DisplayImageOptions options = new DisplayImageOptions
                .Builder()
                .showImageOnLoading(R.drawable.icon_image_loading)//加载中显示的图片
                .showImageOnFail(0)//加载失败时显示的图片
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build();
        ImageLoader.getInstance().displayImage(imgUrl, imageView, options);
    }

    public static void setImageNotCache(String imgUrl, ImageView imageView) {
        //显示图片的配置
        DisplayImageOptions options = new DisplayImageOptions
                .Builder()
                .showImageOnLoading(R.drawable.icon_image_loading)//加载中显示的图片
                .showImageOnFail(0)//加载失败时显示的图片
                .cacheInMemory(false)
                .cacheOnDisk(false)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build();
        ImageLoader.getInstance().displayImage(imgUrl, imageView, options);
    }

    /**
     * 从drawable中异步加载本地图片
     *
     * @param imageId
     * @param imageView
     */
    public static void displayFromDrawable(int imageId, ImageView imageView) {
        // String imageUri = "drawable://" + R.drawable.image; // from drawables
        // (only images, non-9patch)
        ImageLoader.getInstance().displayImage("drawable://" + imageId,
                imageView);
    }

    /**
     * 从内存卡中异步加载本地图片
     *
     * @param uri
     * @param imageView
     */
    public static void displayFromSDCard(String uri, ImageView imageView) {
        // String imageUri = "file:///mnt/sdcard/image.png"; // from SD card
        ImageLoader.getInstance().displayImage("file://" + uri, imageView);
    }

    /**
     * 从assets文件夹中异步加载图片
     *
     * @param imageName
     *            图片名称，带后缀的，例如：1.png
     * @param imageView
     */
    public static void dispalyFromAssets(String imageName, ImageView imageView) {
        // String imageUri = "assets://image.png"; // from assets
        ImageLoader.getInstance().displayImage("assets://" + imageName,
                imageView);
    }

    /**
     * 从内容提提供者中抓取图片
     */
    public void displayFromContent(String uri, ImageView imageView) {
        // String imageUri = "content://media/external/audio/albumart/13"; //
        // from content provider
        ImageLoader.getInstance().displayImage("content://" + uri, imageView);
    }
}
